package com.example.myapplication;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.bean.TeacherInfo;
import com.example.myapplication.callback.LoginCallBack;
import com.example.myapplication.helper.PreferencesHelper;
import com.example.myapplication.ic.ILoginInterface;
import com.example.myapplication.ic.LoginInterfaceImpl;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A login screen that offers login via teacherId/password.
 */
public class LoginActivity extends AppCompatActivity implements LoginCallBack {

    ILoginInterface iLoginInterface;
    private ProgressDialog p;
    private EditText mPasswordView;
    private Button mteacherIdSignInButton;
    private AutoCompleteTextView mteacherIdView;
    private TeacherInfo teacherInfo;

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        assurePlayerInit();
        if(teacherInfo!=null&&!teacherInfo.getTeacher_email().equals("")){
            Intent intent = new Intent();
            intent.setClass(this,WorkInfoActivity.class);
            startActivity(intent);
        }
        p = new ProgressDialog(this);
        p.setMessage("加载中...");
        p.setCanceledOnTouchOutside(false);
        teacherInfo = new TeacherInfo();
        iLoginInterface = new LoginInterfaceImpl();
        mPasswordView = (EditText) findViewById(R.id.password);
        mteacherIdSignInButton = (Button) findViewById(R.id.login);
        mteacherIdView = (AutoCompleteTextView) findViewById(R.id.teacherId);
        mteacherIdSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isTrue = iLoginInterface.judgeUserNameAndPassWord(LoginActivity.this, mteacherIdView, mPasswordView);
                if (isTrue == false) {
                    p.show();
                    iLoginInterface.login(getViewString(mteacherIdView), getViewString(mPasswordView), LoginActivity.this);
                }
            }
        });
        findViewById(R.id.register).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegidterActivity.class);
                LoginActivity.this.startActivity(intent);
            }
        });


    }



   private  String getViewString(EditText view) {
        return view.getText().toString().trim();
    }


    public void saveTeacheer(Activity activity, TeacherInfo teacherInfo){

        PreferencesHelper.writetoPreference(activity,teacherInfo);
    }

    private void assurePlayerInit() {
        if (teacherInfo == null) {
            teacherInfo = PreferencesHelper.getTeacher(this);
        }
    }


    @Override
    public void onSuccess(String info) {
        p.dismiss();
        try {
            JSONObject jsonObject = new JSONObject(info);
            String errcode = jsonObject.optString("errcode");
            Log.i("login",info);
            if ("0".equals(errcode)) {
               // Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
                JSONObject jsonObject1 = jsonObject.optJSONObject("data");
                String teacher_id = jsonObject1.optString("teacher_id");
                String teacher_name = jsonObject1.optString("teacher_name");
                String teacher_number = jsonObject1.optString("teacher_number");
                String teacher_email = jsonObject1.optString("teacher_email");
                String teacher_password = jsonObject1.optString("teacher_password");
                String teacher_pic = jsonObject1.optString("teacher_pic");
                teacherInfo.setTeacher_id(teacher_id);
                teacherInfo.setTeacher_name(teacher_name);
                teacherInfo.setTeacher_email(teacher_email);
                teacherInfo.setTeacher_password(teacher_password);
                teacherInfo.setTeacher_pic(teacher_pic);
                teacherInfo.setTeacher_number(teacher_number);
                saveTeacheer(this,teacherInfo);
                Intent intent = new Intent(LoginActivity.this,WorkInfoActivity.class);
                LoginActivity.this.startActivity(intent);


            } else {
                Toast.makeText(LoginActivity.this, "登陆失败,请重新登陆", Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onFail(String info) {
        p.dismiss();
        Toast.makeText(this, "网络出错,请检查网络", Toast.LENGTH_LONG).show();
    }


}

